#include "head.h"
void func2(int num){
  printf("num:%d\n",num);
}
int initPool(worker_status_t *workerList, int num) {
  for (int i = 0; i < num; i++) {
    int local_fds[2];
    int ret_socketpair = socketpair(AF_LOCAL, SOCK_STREAM, 0, local_fds);
    // local_fds[0]作发送，[1]做接收,互换也行
    ERROR_CHECK(ret_socketpair, -1, "socketpair");

    pid_t pid = fork();
    if (pid == 0) { // 子进程
      // 启动工作
      setpgid(0,0);
      close(local_fds[1]);

      printf("a worker is created(PID = %d), waiting for calling...\n",getpid());

      doWorker(local_fds[0]);
    } else { // 主进程
      // 记录子进程信息
      workerList[i].pid = pid;
      workerList[i].flag = FREE;
      workerList[i].local_socket = local_fds[1];
      close(local_fds[0]);
    }
  }
  return 0;
}

int HandleConnect(worker_status_t *workerList, int num, int net_fd) {
  for (int i = 0; i < num; i++) {
    if (workerList[i].flag == FREE) {
      workerList[i].flag = BUSY;
      sendMesg(workerList[i].local_socket, net_fd,0);
      break;
    }
  }
  return 0;
}
